package com.handinfo.engine.resource;

import java.io.*;

import android.util.Log;

import com.handinfo.engine.Actor;
import com.handinfo.engine.Animation;
import com.handinfo.engine.Background;
import com.handinfo.game.ActorNPC;
import com.handinfo.game.GameScene;
import com.handinfo.util.Tools;

/**
 * @author Xia Mingwei
 * @version 1.0
 */
public class ObjectAdapter
{
	public static final byte RESOURCE_TYPE_PNG = 1;
	public static final byte RESOURCE_TYPE_ANIM = 2;
	public static final byte RESOURCE_TYPE_BG = 3;
	public static final byte RESOURCE_TYPE_ACTOR = 4;
	public static final byte RESOURCE_TYPE_ROLE = 5;
	public static final byte RESOURCE_TYPE_NPC = 6;
	public static final byte RESOURCE_TYPE_ITEM = 7;
	public static final byte RESOURCE_TYPE_SCENE = 8;
	public static final byte RESOURCE_TYPE_SOUND = 9;

	public static final String DIR_IMG = "/img/";
	public static final String DIR_ANIM = "/anim/";
	public static final String DIR_BG = "/bg/";
	public static final String DIR_SCENE = "/scene/";
	public static final String DIR_AC = "/ac/";
	public static final String EXTENSION_IMG = ".png";
	public static final String EXTENSION_ANIM = ".anim";
	public static final String EXTENSION_BG = ".bg";
	public static final String EXTENSION_SCENE = ".scn";
	public static final String EXTENSION_AC = ".ac";
	public static final Serializeable convertPng(Resource res, InputStream is) {
		Serializeable inst = new SerializeableImage();
//		switch (head.m_type) {
//			case RESOURCE_TYPE_PNG:
//				inst = new SerializeableImage();
//				break;
////			case RESOURCE_TYPE_ANIM:
////				inst = new Animation();
////				break;
////			case RESOURCE_TYPE_BG:
////				inst = new Background();
////				break;
////			case RESOURCE_TYPE_SCENE:
////				inst = new GameScene();
////				break;
////			case RESOURCE_TYPE_ACTOR:
////				inst = new Actor();
////				break;
////			case RESOURCE_TYPE_NPC:
////				inst = new ActorNPC();
////				break;
//		}
		try {
			inst.read(res, new DataInputStream(is));
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return inst;
	}
	public static final Serializeable convert(Resource res, ResHead head,
			byte[] data)
	{
		Serializeable inst = null;
		switch (head.m_type) {
			case RESOURCE_TYPE_PNG:
				inst = new SerializeableImage();
				break;
			case RESOURCE_TYPE_ANIM:
				inst = new Animation();
				break;
			case RESOURCE_TYPE_BG:
				inst = new Background();
				break;
			case RESOURCE_TYPE_SCENE:
				inst = new GameScene();
				break;
			case RESOURCE_TYPE_ACTOR:
				inst = new Actor();
				break;
			case RESOURCE_TYPE_NPC:
				inst = new ActorNPC();
				break;
		}

		try {
			
			if (head.m_type == RESOURCE_TYPE_PNG) {
				Log.d("Png", "file="+head.m_url);
				((SerializeableImage)inst).read(res, data);
			} else {
				ByteArrayInputStream bas = new ByteArrayInputStream(data);
				DataInputStream dis = new DataInputStream(bas);
				inst.read(res, dis);
				dis.close();
				bas.close();
			}
//			}
			
		}
		catch (Exception ex) {
			Tools.debugPrintln("type : " + head.m_type);
			Tools.debugPrintln("url : " + head.m_url);
			Tools.debug(ex);
			return null;
		}
		return inst;
	}

	// /**
	// * 根据后缀名获取资源类型
	// * @param url String
	// * @return int
	// */
	// public static byte getResourceType(String url)
	// {
	// String extension = url.substring(url.indexOf("."));
	// if (extension.equals(".png")) {
	// return RESOURCE_TYPE_PNG;
	// }
	// else if (extension.equals(".anim")) {
	// return RESOURCE_TYPE_ANIM;
	// }
	// else if (extension.equals(".ac")) {
	// return RESOURCE_TYPE_ACTOR;
	// }
	// else if (extension.equals(".bg")) {
	// return RESOURCE_TYPE_BG;
	// }
	// else if (extension.equals(".scn")) {
	// return RESOURCE_TYPE_SCENE;
	// }
	// System.out.println("can not find the resource type :" + extension);
	// return -1;
	// }

	private ObjectAdapter()
	{
	}
}
